<template>
  <div class="app-container">



    <div solt="title" class="custom-title">
      <el-form style="float: right; top: 10px;" :model="queryParams" ref="queryForm" size="small" :inline="true"
        v-show="showSearch" label-width="8px">
        <el-form-item label="" prop="state">
          <el-select style="width: 120px;" @change="getList()" v-model="queryParams.state" placeholder="处理状态" clearable>
            <el-option v-for="dict in dict.type.waring_state" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
      </el-form>
    </div>

    <!-- 操作按钮行 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">批量删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-refresh" size="mini" @click="getList">刷新</el-button>
      </el-col>
    </el-row>

    <!-- 表格展示 -->
    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="位置信息" align="left" prop="cellName" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ formatRoomAddress(scope.row) }}
        </template>
      </el-table-column>
      <el-table-column label="报警类型" align="center" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.waring_type" :value="scope.row.waringType" />
        </template>
      </el-table-column>
      <el-table-column label="上报内容" align="left" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="处理状态" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.waring_state" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column label="处理结果" align="left" prop="remark2" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">处理</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <el-empty :image-size="100" style="height: 80vh;" :image="emty"
      v-if="!infoList || infoList.length === 0"></el-empty>

    <!-- 添加或修改预警信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="报警类型" prop="waringType">
              <el-select v-model="form.waringType" placeholder="请选择报警类型">
                <el-option v-for="dict in dict.type.waring_type" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处理状态" prop="state">
              <el-select v-model="form.state" placeholder="请选择处理状态">
                <el-option v-for="dict in dict.type.waring_state" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="上报问题" prop="remark">
          <el-input v-model="form.remark" type="textarea" minlength="3" maxlength="30" show-word-limit
            placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="处理结果" prop="remark2">
          <el-input v-model="form.remark2" type="textarea" minlength="3" maxlength="30" show-word-limit
            placeholder="请输入处理结果" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listInfo,
    getInfo,
    delInfo,
    addInfo,
    updateInfo
  } from "@/api/lock/info";
  import emty from "@/assets/images/emty/暂无数据2.png";

  export default {
    name: "Info",
    dicts: ['waring_state', 'waring_type'],
    data() {
      return {
        emty: emty,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 预警信息表格数据
        infoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          roomid: null,
          mac: null,
          waringType: null,
          state: "0",
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          remark: [{
            required: true,
            message: "上报问题不能是空",
            trigger: "blur"
          }],
          remark2: [{
            required: true,
            message: "反馈问题不能是空",
            trigger: "blur"
          }],
          waringType: [{
            required: true,
            message: "报警类型必须填",
            trigger: "change"
          }],
          state: [{
            required: true,
            message: "处理状态必须填",
            trigger: "change"
          }],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询预警信息列表 */
      getList() {
        this.loading = true;
        listInfo(this.queryParams).then(response => {
          this.infoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 格式化房间地址显示
      formatRoomAddress(item) {
        let address = '';
        if (item.cellName) {
          address += item.cellName;
        }
        if (item.unitName) {
          address += '-' + item.unitName;
        }
        if (item.floorName) {
          address += '-' + item.floorName + '层';
        }
        if (item.roomName) {
          address += '-' + item.roomName + '房间';
        }
        return address || '未知位置';
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          roomid: null,
          mac: null,
          waringType: null,
          state: null,
          remark: null,
          remark2: null,
          createTime: null,
          updateTime: null,
          updateBy: null,
          createBy: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加预警信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        const title = this.formatRoomAddress(row);
        getInfo(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = title || "修改预警信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateInfo(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addInfo(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除预警信息编号为"' + ids + '"的数据项？').then(function() {
          return delInfo(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('lock/info/export', {
          ...this.queryParams
        }, `info_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>

<style lang="scss" scoped>
  .custom-title {
    height: 60px;
    background: white;
    color: #201e1e;
    padding: 10px;
    margin-bottom: 15px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
    border-radius: 4px;
  }

  .el-table {
    margin-bottom: 15px;
    border-radius: 4px;
    overflow: hidden;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
  }

  .mb8 {
    margin-bottom: 8px;
  }

  .app-container {
    padding: 15px;
    background-color: #f5f7fa;
  }

  .el-button--text {
    color: #409EFF;
    &:hover {
      color: #66b1ff;
    }
  }
</style>
